﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Globalization;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wRouteDeparture.xaml
    /// </summary>
    public partial class wRouteDeparture : Window
    {
        routelistEntities db;
        IEditableCollectionView vw;

        public RouteTable rtable { get; set; }

        public wRouteDeparture()
        {
            InitializeComponent();
            db = new routelistEntities();
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            if (null == db) return;
            RouteTable rtl = (RouteTable)vw.AddNew();
            Schedule item = (from o in db.Schedule where  o.monday == true && o.tuesday == true && o.wednesday == true && o.thursday == true && o.friday == true && o.saturday  == false && o.sunday  == false select o).FirstOrDefault();
            if (null == item)
            {
                item = new Schedule();
                item.monday = true;
                item.tuesday = true;
                item.wednesday = true;
                item.thursday = true;
                item.friday = true;
                item.saturday = false;
                item.sunday = false;
                db.Schedule.AddObject(item);
                SaveData();
            }
            rtl.numberDeparture = 0;
            rtl.id_schedule = item.id;
            rtl.departure = "";
            rtl.arrive = "";
            rtl.total_length = 0.0f;
            rtl.total_free_length = 0.0f;
            rtl.total_reis = 0;
            rtl.total_work_time = 0;
            rtl.total_rest_time = 0;
            rtl.sum_plan = 0;
            //db.RouteTable.AddObject(rtl);
            vw.CommitNew();
            listView1.ScrollIntoView(rtl);
            listView1.SelectedItem = rtl;
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                InitData();
            }

            
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {

        }

        private void buttonEdit_Click(object sender, RoutedEventArgs e)
        {
            if (null == vw) return;
            RouteTable curItem = listView1.SelectedItem as RouteTable;
            if (null == curItem) return;
            wFullRouteInfo wnd = new wFullRouteInfo();
            wnd.ContextDB = db;
            wnd.route_table = curItem;
            wnd.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitData();
        }

        private void InitData()
        {
            CollectionViewSource cvs = (CollectionViewSource)FindResource("sourceDeparture");
            cvs.Source = from o in db.RouteTable orderby o.numberDeparture, o.id_schedule select o;
            vw = (IEditableCollectionView)cvs.View;
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            if (null == db)
            {
                MessageBox.Show("Изменения не сохранены");
                return;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {

        }

        protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
        {
            ListViewItem item = (ListViewItem)sender;
            item.IsSelected = true;
            SaveData();
        }

        private void SaveData()
        {
            if (null == db)
            {
                MessageBox.Show("Изменения не сохранены");
                return;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

    [ValueConversion(typeof(Schedule), typeof(String))]
    public class ScheduleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Schedule item = value as Schedule;
            if (item == null) return "";
            StringBuilder result = new StringBuilder();
            if (item.monday == true) result.Append("ПН");
            if (item.tuesday == true) result.Append(" ВТ");
            if (item.wednesday == true) result.Append(" СР");
            if (item.thursday == true) result.Append(" ЧТ");
            if (item.friday == true) result.Append(" ПТ");
            if (item.saturday == true) result.Append(" СБ");
            if (item.sunday == true) result.Append(" ВС");
            return result.ToString().Trim();
        }




        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
